clear
set more off
capture log close
cd "P:\2018\186"

gl data_dir "P:\2018\186/use"
gl save_dir "P:\2018\186/Martin/Decompositions_SE_extLLSMM/Grandparents"

  *child ages to observe their adult incomes
  gl agemin=25 // youngest possible=25
  gl agemax=48 // oldest possible=48
  
  *child birth cohorts to use
  loc cohortmin=1952 // earliest possible=1952
  loc cohortmax=1994 // latest possible=1993
  
log using ${save_dir}/causal_vs_measurementerror.log, replace


  
*USE the ranked MAIN SAMPLE 
use *idnr *earn* *yob *obsy *max* *edu* *emp* year woman if year>=1985 using "use\sample_usswe_ls", replace

* Rename variable names

rename pmearn pm_LAB
rename pfearn pf_LAB
rename pearn pLAB

rename memp m_emplavg
rename femp f_emplavg
rename emp employ

rename medu m_schmax
rename fedu f_schmax
rename edu schmax

ren idnr newid
ren midnr m_newid
ren fidnr f_newid

gen AGEC=year-yob		// Age variables and normalization
gen AGEC1=AGEC-40		
g f_LABAGE_1=fobsy-fyob
g m_LABAGE_1=mobsy-myob
*egen fmage1=rowmean(agef1 agem1)
forval i=2/4{
	g AGEC`i'=AGEC1^`i'
	g f_LABAGE_`i'=f_LABAGE_1^`i'
	g m_LABAGE_`i'=m_LABAGE_1^`i'
	*g fmage`i'=fmage1^`i'
   }
  * MOTHER's SAMPLE
 keep if pm_LAB!=. & m_LABAGE_1!=.
 keep if m_emplavg!=. & m_schmax!=. 
 keep if schmax!=.
 
* FATHER's SAMPLE
 keep if pf_LAB!=. & f_LABAGE_1!=.
 keep if f_emplavg!=. & f_schmax!=. 
 keep if schmax!=.
 

/* RESDIUALIZE all measures 
 foreach var in pLAB employ schmax pm_LAB  m_schmax pf_LAB  f_schmax {
	qui regress `var' m_LABAGE_? f_LABAGE_? AGEC? i.year 
	predict `var'_r, resid
	drop `var'
	rename `var'_r `var'
 }
*/ 



************************
* MOTHERS 

**** Early period   
preserve

loc yrmin=1985
loc yrmax=1995

* Subsample 
 keep if inrange(year,`yrmin',`yrmax') & inrange(AGEC,${agemin},${agemax})
 
*1 IRP
 regress pLAB pm_LAB m_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp =_b[pm_LAB]
 matrix se =_se[pm_LAB]
 matrix R =e(r2)
 matrix N =e(N)
 
*2 IRP conditional on mother's education
 regress pLAB pm_LAB m_schmax m_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pm_LAB]
 matrix se= se, _se[pm_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)

*3 IRP conditional on mother's education and father's income and education
 regress pLAB pm_LAB m_schmax pf_LAB f_schmax m_LABAGE_? f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pm_LAB]
 matrix se= se, _se[pm_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
restore 

**** Late period  
preserve

loc yrmin=2008
loc yrmax=2019

* Subsample 
 keep if inrange(year,`yrmin',`yrmax') & inrange(AGEC,${agemin},${agemax})
 
*4 IRP
 regress pLAB pm_LAB m_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pm_LAB]
 matrix se= se, _se[pm_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
*5 IRP conditional on mother's education
 regress pLAB pm_LAB m_schmax m_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pm_LAB]
 matrix se= se, _se[pm_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)

*6 IRP conditional on mother's education and father's income and education
 regress pLAB pm_LAB m_schmax pf_LAB f_schmax m_LABAGE_? f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pm_LAB]
 matrix se= se, _se[pm_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
restore  




************************
* FATHERS 

***** Early period  
preserve

loc yrmin=1985
loc yrmax=1995

* Subsample 
 keep if inrange(year,`yrmin',`yrmax') & inrange(AGEC,${agemin},${agemax})
 
*7 IRP
 regress pLAB pf_LAB f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
*8 IRP conditional on father's education
 regress pLAB pf_LAB f_schmax f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)

 
*9 IRP conditional on father's education and mother's income and education
 regress pLAB pf_LAB f_schmax pm_LAB m_schmax m_LABAGE_? f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
restore 

***** Late period for fathers 
preserve

loc yrmin=2008
loc yrmax=2019

* Subsample 
 keep if inrange(year,`yrmin',`yrmax') & inrange(AGEC,${agemin},${agemax})
 
*10 IRP
 regress pLAB pf_LAB f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
*11 IRP conditional on father's education
 regress pLAB pf_LAB f_schmax f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)

*12 IRP conditional on father's education and mother's income and education
 regress pLAB pf_LAB f_schmax pm_LAB m_schmax m_LABAGE_? f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
restore  
 
 


**** Save results to datset 

clear 
set obs 1
gen temp=1 
 svmat irp
 svmat se
 svmat R 
 svmat N 
 reshape long irp se R N, i(temp) j(regnum) 
 drop temp 

gen mother=. 	// mother IRP?
replace mother=1 if inrange(regnum,1,6)
replace mother=0 if inrange(regnum,7,12)

gen period=.	// time period
replace period=1 if inlist(regnum,1,2,3,7,8,9)
replace period=2 if inlist(regnum,4,5,6,10,11,12)

gen specif=.	// specification (3 for each parent/period)
replace specif=1 if inlist(regnum,1,4,7,10)
replace specif=2 if inlist(regnum,2,5,8,11)
replace specif=3 if inlist(regnum,3,6,9,12)

label define regnum_vals ///
1 "IRP" ///
2 "IRP conditional on mother's education" ///
3 "IRP conditional on mother's education and father's income and education" ///
4 "IRP" ///
5 "IRP conditional on mother's education" ///
6 "IRP conditional on mother's education and father's income and education" ///
7 "IRP" ///
8 "IRP conditional on father's education" ///
9 "IRP conditional on father's education and mother's income and education" ///
10 "IRP" ///
11 "IRP conditional on father's education" ///
12 "IRP conditional on father's education and mother's income and education"

label values regnum regnum_vals 
order regnum mother period specif, first 
 
qui compress
save ${save_dir}/SE_irps.dta, replace



*Look at % change

preserve 
keep if mother==1
keep irp mother period specif R 
reshape wide irp R, i(specif) j(period)
gen pct_change=(irp2-irp1)/irp1
di _newline(2) "Mothers"
list 
restore 

preserve 
keep if mother==0
keep irp mother period specif R 
reshape wide irp R, i(specif) j(period)
gen pct_change=(irp2-irp1)/irp1
di _newline(2) "Fathers"
list 
restore

clear
log close 

